package dry.example.gui;

import javax.swing.*;
import java.awt.*;

public class CardLayoutDemo {

    public static void main(String[] args) {
        // 创建主窗口
        JFrame frame = new JFrame("CardLayout 示例");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);

        // 创建一个使用 CardLayout 的面板
        final JPanel cardsPanel = new JPanel(new CardLayout());
        cardsPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 创建几个卡片（JPanel）
        JPanel card1 = new JPanel();
        card1.add(new JLabel("这是卡片 1"));
        card1.setBackground(Color.LIGHT_GRAY);

        JPanel card2 = new JPanel();
        card2.add(new JLabel("这是卡片 2"));
        card2.setBackground(Color.PINK);

        JPanel card3 = new JPanel();
        card3.add(new JTextArea("这是卡片 3\n你可以在这里放置更多的组件..."));
        card3.setBackground(Color.CYAN);

        // 将卡片添加到卡片面板中，并指定名称
        cardsPanel.add(card1, "Card 1");
        cardsPanel.add(card2, "Card 2");
        cardsPanel.add(card3, "Card 3");

        // 创建按钮面板，用于控制卡片切换
        JPanel buttonPanel = new JPanel();
        JButton prevButton = new JButton("上一张");
        JButton nextButton = new JButton("下一张");

        // 添加按钮事件监听器
        final CardLayout cardLayout = (CardLayout)(cardsPanel.getLayout());
        prevButton.addActionListener(e -> cardLayout.previous(cardsPanel));

        nextButton.addActionListener(e -> cardLayout.next(cardsPanel));

        buttonPanel.add(prevButton);
        buttonPanel.add(nextButton);

        // 将卡片面板和按钮面板加入主窗口
        frame.setLayout(new BorderLayout());
        frame.add(cardsPanel, BorderLayout.CENTER);
        frame.add(buttonPanel, BorderLayout.SOUTH);

        // 显示窗口
        frame.setVisible(true);
    }

}